// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// S [author] rhyme_lph
// s [email]  rhymelph@gmail.com
// C [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/http_service/http_login_service.dart';
import 'package:finance_app/src/widgets/bottom_sheet/got_it_tip_bottom_sheet.dart';
import 'package:flutter/material.dart';

class RequestFreeDemoProvider extends BaseProvider {
  TextEditingController businessNameController = TextEditingController();
  FocusNode businessNameFocusNode = FocusNode();

  @override
  Future get dataFuture => null;

  TextEditingController nameController = TextEditingController();
  TextEditingController emailController = TextEditingController();
  TextEditingController phoneNumberController = TextEditingController();
  String countryCode = '+65';

  bool isCheckPrivacyPolicy = false;
  bool enable = false;

  @override
  void initState() {
    super.initState();
    final callback = () {
      if (nameController.text.isNotEmpty &&
          emailController.text.isNotEmpty &&
          phoneNumberController.text.isNotEmpty) {
        if (enable == false) {
          enable = true;
          update();
        }
      } else {
        if (enable == true) {
          enable = false;
          update();
        }
      }
    };
    nameController.addListener(callback);
    emailController.addListener(callback);
    phoneNumberController.addListener(callback);
  }

  @override
  void dispose() {
    super.dispose();
    nameController.dispose();
    emailController.dispose();
    phoneNumberController.dispose();
  }

  void onChangeCountryCode(value) {
    FocusManager.instance.primaryFocus.unfocus();
    if (value != countryCode) countryCode = value;
  }

  void onChangePrivacyCheck() {
    FocusManager.instance.primaryFocus.unfocus();
    isCheckPrivacyPolicy = !isCheckPrivacyPolicy;
    update();
  }

  void onSubmit() async {
    FocusManager.instance.primaryFocus.unfocus();
    if (nameController.text.length == 0) {
      showToast(S.current.please_enter_name);
      return;
    }
    if (emailController.text.length == 0) {
      showToast(S.current.please_enter_email);
      return;
    }

    if (phoneNumberController.text.length == 0) {
      showToast(S.current.please_enter_a_valid_mobile_number);
      return;
    }

    bool isSuccess = false;
    await post(
        HttpLoginService.requestDemo(nameController.text, emailController.text,
            countryCode, phoneNumberController.text),
        autoHandler: AutoHandler.Toast,
        loadingTip: S.current.loading, onSuccess: (result) {
      isSuccess = true;
    });

    if (isSuccess == true)
      obtainContext((context) async {
        await showGotItTipBottomSheet(
          context,
          S.current.we_will_contact_you_in_a_short_while,
        );
        Navigator.pop(context);
      });
  }
}
